<?php

/**
 * candidate actions.
 *
 * @package    lomejorde
 * @subpackage candidate
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class candidateActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    {
        $this->pager = new sfDoctrinePager('Candidate', 15);
        
        $this->pager->setQuery($this->setPagerQuery($request));
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->init();
        
        $this->categories = Doctrine::getTable('Category')->findAll();
    }

    public function executeShow(sfWebRequest $request)
    {
        $this->candidate = $this->getRoute()->getObject();
    }
    
    public function executeShowNominations(sfWebRequest $request)
    {
        $this->candidate = $this->getRoute()->getObject();
        
        $categories = $this->candidate->getCategories();
        
        $this->totals = array();
        
        foreach ($categories as $key => $category) {
            $this->totals[$key]['category'] = $category;
            
            $query = Doctrine_Query::create()->from('Nomination n');
        
            $query->andWhere('n.candidate_id = ?', $this->candidate->getId());
            
            $query->andWhere('n.category_id = ?', $category->getId());
            
            $this->totals[$key]['total'] = $query->count();
        }
    }

    public function executeNew(sfWebRequest $request)
    {
        $this->form = new CandidateBackendForm();
    }

    public function executeCreate(sfWebRequest $request)
    {
        $this->form = new CandidateBackendForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request)
    {
        $this->form = new CandidateBackendForm($this->getRoute()->getObject());
    }

    public function executeUpdate(sfWebRequest $request)
    {
        $this->form = new CandidateBackendForm($this->getRoute()->getObject());

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request)
    {
        $request->checkCSRFProtection();

        $this->getRoute()->getObject()->delete();

        $this->redirect('@candidate');
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $candidate = $form->save();
            
            $this->getUser()->setFlash('notice', 
                    sprintf('Se ha guardado la información sobre el candidato %s de forma exitosa.', $candidate->getName())
                );

            $this->redirect('@candidate_show?id='.$candidate->getId());
        }
    }
    
    public function executeSearch(sfWebRequest $request)
    {
        $this->pager = new sfDoctrinePager('Candidate', 15);
        
        $this->pager->setQuery($this->setPagerQuery($request));
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->init();

        if ($request->isXmlHttpRequest())
        {
            if (!$this->pager->getResults())
            {
                return $this->renderText('No hay resultados que coincidan con la busqueda.');
            }
            
            return $this->renderPartial('list', array('pager' => $this->pager, 'query' => $request->getParameter('query','')));
        } 
        else 
        {
            $this->setTemplate('index');
        }
    }
    
    private function setPagerQuery(sfWebRequest $request) {
        $query = Doctrine_Query::create()->from('Candidate')->orderBy('name');
        
        $str = $request->getParameter('query','');
        
        if($str !== ''){
            $query = Doctrine_Core::getTable('Candidate')->addCandidatesByNameOrDescriptionQuery($str, $query);
        }
        
        $categories = $request->getParameter('categories', array());
        
        if(is_array($categories) && count($categories)){
            $query = Doctrine_Core::getTable('Candidate')->addCandidatesByCategoriesQuery($categories, $query);
        }
        
        $status = $request->getParameter('status', 2);
        
        if($status < 2){
            $query = Doctrine_Core::getTable('Candidate')->addCandidatesStatusQuery($status == 0 ? false : true, $query);
        }
        
        return $query;
    }
}
